package 蓝桥杯算法;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.stream.IntStream;

public class day19 {
    public static void main(String[] args) {
        day19 a=new day19();
        int[] arr={2,3,1,1,4};
        System.out.println(a.covering(arr));
        int[] arr1={3,-1,0,2};
        int[] arr2={1,0,2};
        System.out.println(a.largest(arr1,3));
        System.out.println(a.candy(arr2));
        int[] charge={10,10};
        System.out.println(a.lemonade(charge));

    }

   public  int covering(int[] arr){
   int count=0;
   int curDistence=0;
   int maxDistence=0;
   for(int i=0;i<arr.length;i++){
       maxDistence=Math.max(maxDistence,i+arr[i]);
       if(maxDistence>=arr.length-1){
           count++;
           break;

       }
       if(i==curDistence){//找到了目前最大值的位置
           curDistence=maxDistence;
           count++;
       }
   }
   return count;
   }
   public int largest(int[] arr,int key){
       Arrays.sort(arr);
       for(int i=0;i<arr.length&&key>0;i++){
           if(arr[i]<0){
               arr[i]=-arr[i];
               key--;
           }
       }
       if(key>0){
           Arrays.sort(arr);
           arr[0]=-arr[0];//选择整数中最小的调节
       }
       int sum=0;
       for(int i=0;i<arr.length;i++){
           sum=sum+arr[i];
       }
           return sum;
       }
       public int cancomplete(int[] gas,int[] cost) {
    int sum=0;
    int min=0;
    for(int i=0;i<gas.length;i++){
        min+=gas[i]-cost[i];
        if(min>0){
            return i;
        }
    }
    return -1;
    }
    public int canComplete(int[] gas,int[] cost){//
        int cursum=0;
        int totalsum=0;
        int index=0;
        for(int i=0;i<gas.length;i++){
            cursum+=gas[i]-cost[i];
            totalsum+=gas[i]-cost[i];
            if(cursum<0){
                index=(i+1)%gas.length;
                cursum=0;
            }
        }
        if(totalsum<0){
            return -1;
        }
        return index;
    }
    public int candy(int[] student){//左右循环均兼顾
        int sum=0;
        int[] candy=new int[student.length];
        candy[0]=1;
       for(int i=1;i<student.length;i++){
         if(student[i]>student[i-1]){
             candy[i]=candy[i-1]+1;
         }
         else if(student[i]<=student[i-1]){
             candy[i]=1;
         }
       }
       for(int i=student.length-2;i>=0;i--) {
          if(student[i]>student[i+1]){
              candy[i]=candy[i+1]+1;
          }
          else{
              candy[i]=1;
          }
       }
       for(int i:candy){
           sum=sum+i;
       }
      return sum;
    }
    public boolean lemonade(int[] charge){
        int[] money=new int[3];
        for(int i=0;i<charge.length;i++){
            if(charge[i]==5){
                money[0]=money[0]+1;
            }
            else if(charge[i]==10){
                if(money[0]>0){
                    money[0]=money[0]+1;
                    money[1]=money[1]+1;
                }
                else {
                    return false;
                }
            }
            else if(charge[i]==20){
                if(money[1]>0&&money[1]>0){
                    money[0]=money[0]-1;
                    money[1]=money[1]-1;
                    money[2]=money[2]+1;
                }
                else if(money[1]>3){
                    money[0]=money[0]-3;
                }
                else{
                    return false;
                }

            }

        }
        return true;
    }
    public int[][] reconstruct(int[][] people){
       Arrays.sort(people,(a,b)->{//先对前者排序，如果相同就对后者排序!!
           if(a[0]==b[0]) return a[1]-b[1];
           return b[0]-a[0];
       });
        LinkedList<int[]> que=new LinkedList<>();
       for(int[] p:people){
           que.add(p[1],p);
       }
       return que.toArray(new int[people.length][]);
    }


}
